package cn.itrigger.publish;

import cn.itrigger.config.VersionConfig;
import cn.itrigger.utils.CommandLineUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;

public abstract class VersionPublish<R> {

    public abstract R publish() throws MojoExecutionException;

    /**
     * deploy到nexus
     * @param config
     */
    public void deployToNexus(VersionConfig config) {
        StringBuffer sb = new StringBuffer("mvn clean deploy -Dmaven.test.skip=true ");
        if (StringUtils.isNotBlank(config.getModule())) {
            sb.append(" -pl ").append(config.getModule());
        }
        if (!Boolean.valueOf(config.getHasInstall())) {
            sb.append(" -am ");
        }
        if (null != config.getEnv()) {
            sb.append(" -P ").append(config.getEnv());
        }
        if (null != config.getNexusUrl()) {
            sb.append(" -DaltDeploymentRepository=").append(config.getNexusUrl());
        }
        System.out.println("sb: " + sb.toString());
        String result = CommandLineUtils.execCmdWithResult(sb.toString());
        System.out.println(result);
    }

    public void packageToLocal(VersionConfig config) {
        StringBuffer sb = new StringBuffer("mvn clean package -Dmaven.test.skip=true ");
        if (StringUtils.isNotBlank(config.getModule())) {
            sb.append(" -pl ").append(config.getModule());
        }
        if (!Boolean.valueOf(config.getHasInstall())) {
            sb.append(" -am ");
        }
        if (null != config.getEnv()) {
            sb.append(" -P ").append(config.getEnv());
        }

        System.out.println(sb.toString());
        String result = CommandLineUtils.execCmdWithResult(sb.toString());
        System.out.println(result);
    }

    /**
     * 解决本地parent模块读取不到的问题
     */
    public void install() {
        StringBuilder sb = new StringBuilder("mvn clean install -U -Dmaven.test.skip=true");
        String result = CommandLineUtils.execCmdWithResult(sb.toString());
        System.out.println(result);
    }
}
